﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SeriesWatcher.DataModel
{
    class Serial
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int startYear;
        public int StartYear
        {
            get { return startYear; }
            set { startYear = value; }
        }

        private int endYear;
        public int EndYear
        {
            get { return endYear; }
            set { endYear = value; }
        }

        private bool ended;
        public bool Ended
        {
            get { return ended; }
            set { ended = value; }
        }

        private List<Season> seasons;

        public List<Season> Seasons
        {
            get { return seasons; }
            set { seasons = value; }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public Serial()
        {
            this.name = "";
            this.startYear = 1900;
            this.endYear = -1;
            this.ended = false;
            this.seasons = new List<Season>();
        }

        /// <summary>
        /// Adding season
        /// </summary>
        /// <returns>Pointer on new season</returns>
        public Season AddSeason()
        {
            Season season = new Season();
            seasons.Add(season);
            return season;
        }

        public Season AddSeason(int number)
        {
            if (number < 0)
                return null;

            foreach (Season s in seasons)
            {
                if (s.Number == number)
                    return null;
            }
            Season season = new Season(number);
            seasons.Add(season);
            return season;
        }

        public XmlElement SaveToXml(XmlDocument doc)
        {
            XmlElement element = doc.CreateElement("serial");
            XmlAttribute att0 = doc.CreateAttribute("name");
            XmlAttribute att1 = doc.CreateAttribute("startYear");
            XmlAttribute att2 = doc.CreateAttribute("endYear");
            XmlAttribute att3 = doc.CreateAttribute("ended");

            att0.Value = Name;
            att1.Value = StartYear.ToString();
            att2.Value = EndYear.ToString();
            att3.Value = Ended.ToString();

            element.SetAttributeNode(att0);
            element.SetAttributeNode(att1);
            element.SetAttributeNode(att2);
            element.SetAttributeNode(att3);

            foreach (Season season in Seasons)
            {
                element.AppendChild(season.SaveToXml(doc));
            }

            return element;

        }


    }
}
